/************************************************************
  *  * EaseMob CONFIDENTIAL 
  * __________________ 
  * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. 
  *  
  * NOTICE: All information contained herein is, and remains 
  * the property of EaseMob Technologies.
  * Dissemination of this information or reproduction of this material 
  * is strictly forbidden unless prior written permission is obtained
  * from EaseMob Technologies.
  */

#import "ESFacialInputView.h"
#import "ESFacialPageController.h"
#import "ESFacialTypeView.h"
#import "Emoji.h"
#import "ESFacial.h"

@interface ESFacialInputView () <ESFacialTypeViewDelegate>
/** 表情 pageVC */
@property (nonatomic,strong) ESFacialPageController *facialPageVC;
/** 表情类型 */
@property (nonatomic,strong) ESFacialTypeView *facialTypeView;
/** 表情字典 */
@property (nonatomic,strong) NSMutableDictionary *facialDict;

@end

@implementation ESFacialInputView
@synthesize textView = _textView;

#pragma mark - 懒加载
- (NSDictionary *)facialDict{
    if (_facialDict == nil) {
        _facialDict = [NSMutableDictionary dictionary];
        // 添加系统默认表情
        NSMutableArray *emojiFacial = [NSMutableArray array];
        for (NSString *emoji in [Emoji allEmoji]) {
            ESFacial *facial = [[ESFacial alloc] init];
            facial.emoji = emoji;
            [emojiFacial addObject:facial];
        }
        [_facialDict setObject:emojiFacial forKey:@"emoji"];
        
        NSMutableArray *emojiFacial2 = [NSMutableArray array];
        int num = 0;
        for (NSString *emoji in [Emoji allEmoji]) {
            ESFacial *facial = [[ESFacial alloc] init];
            facial.emoji = emoji;
            [emojiFacial2 addObject:facial];
            num ++;
            if (num == 20) {
                break;
            }
        }
        [_facialDict setObject:emojiFacial2 forKey:@"emoji2"];
    }
    return _facialDict;
}

#pragma mark - 系统方法
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor lightGrayColor];
    }
    return self;
}

- (instancetype)initWithTextView:(UITextView *)textView {
    self = [super init];
    if (self) {
        _textView = textView;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    ESFacialPageController *facialPageVC = [[ESFacialPageController alloc] initWithTextView:self.textView];
    UIView *facialPageView = facialPageVC.view;
    facialPageView.translatesAutoresizingMaskIntoConstraints = NO;
    facialPageVC.facialDict = self.facialDict;
    [self addSubview:facialPageView];
    self.facialPageVC = facialPageVC;
    
    ESFacialTypeView *facialTypeView = [[ESFacialTypeView alloc] init];
    facialTypeView.delegate = self;
    facialTypeView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:facialTypeView];
    facialTypeView.backgroundColor = [UIColor greenColor];
    facialTypeView.facialTypes = [self.facialDict allKeys];
    self.facialTypeView = facialTypeView;
    
    NSDictionary *subviews = NSDictionaryOfVariableBindings(facialPageView, facialTypeView);
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[facialPageView]|" options:NSLayoutFormatAlignAllLeading | NSLayoutFormatAlignAllTrailing metrics:nil views:subviews]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[facialPageView][facialTypeView(40)]|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:subviews]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[facialTypeView]|" options:NSLayoutFormatAlignAllLeading | NSLayoutFormatAlignAllTrailing metrics:nil views:subviews]];
    
}

#pragma mark - public
- (CGFloat)frameHeight {
    return 200;
}

#pragma mark - ESFacialTypeViewDelegate
- (void)facialTypeViewDidSelectedSend:(ESFacialTypeView *)facialTypeView {
    // 表情键盘上点击了发送按钮
    
}

- (void)facialTypeView:(ESFacialTypeView *)facialTypeView didSelectedType:(NSString *)type {
    [self.facialPageVC setCurrentFacialType:type];
}

@end
